﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using SuperStarProject.Models;
using System.Data.SqlClient;
using SuperStarProject.Models;
namespace SuperStarProject.DAO
{
    public class KhachHangDAO
    {
        public static List<RegisterModel> ShowAll()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("sp_select_DaiLy", db);
            return ConvertRegisterModel(rdr);
        }
        static List<RegisterModel> ConvertRegisterModel(SqlDataReader rdr)
        {
            List<RegisterModel> lstItem = new List<RegisterModel>();
            while (rdr.Read())
            {
                RegisterModel item = new RegisterModel();
                item.UserName = (string)rdr["MaKH"];
                item.Password = (string)rdr["Password"];
                item.Email = (string)rdr["Email"];
                item.PhoneNumber = (string)rdr["SoDt"];
                item.CMND = (string)rdr["CMND"];
                item.HoTen = (string)rdr["HoTen"];
                lstItem.Add(item);
            }
            return lstItem;
        }
        static List<LogOnModel> ConvertLogOnModel(SqlDataReader rdr)
        {
            List<LogOnModel> lstItem = new List<LogOnModel>();
            while (rdr.Read())
            {
                LogOnModel item = new LogOnModel();
                item.UserName = (string)rdr["MaKH"];
                item.Password = (string)rdr["Password"];
                lstItem.Add(item);
            }
            return lstItem;
        }
        public static bool ValidateUser(string UserName, string Password)
        {
            ProductModel prod = new ProductModel();

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@ma", (object)UserName));
            lstParameter.Add(new SqlParameter("@pass", (object)Password));

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_KH_ByUsernameAndPasswork", db, lstParameter);
            if (rdr.HasRows)
                return true;
            else
                return false;
        }
        public static MembershipCreateStatus CreateUser(Models.RegisterModel model)
        {
            ProductModel prod = new ProductModel();

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@ma", (object)model.UserName));

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_KH_dk", db, lstParameter);
            if (rdr.HasRows)
            {              
                return MembershipCreateStatus.DuplicateUserName;
            }
            rdr.Close();
            lstParameter.Clear();
            lstParameter.Add(new SqlParameter("@email", (object)model.Email));
            rdr = DataProvider.RunStoredProcParams_Read("sp_select_KH_ByEmail", db, lstParameter);
            if (rdr.HasRows)
            {
                rdr.Close();
                return MembershipCreateStatus.DuplicateEmail;
            }
            else
            {
                rdr.Close();
                lstParameter.Clear();
                lstParameter.Add(new SqlParameter("@makh", (object)model.UserName));
                lstParameter.Add(new SqlParameter("@email", (object)model.Email));
                lstParameter.Add(new SqlParameter("@pass", (object)model.Password));
                lstParameter.Add(new SqlParameter("@sodt", (object)model.PhoneNumber));
                lstParameter.Add(new SqlParameter("@cmnd", (object)model.CMND));
                lstParameter.Add(new SqlParameter("@hoten", (object)model.HoTen));
                int  result = DataProvider.RunStoredProcParams_NoRead("sp_insert_KhachHang", db, lstParameter);
                return MembershipCreateStatus.Success;
            }
        }
        public static RegisterModel GetProfile(string accountName)
        {
            RegisterModel prod = new RegisterModel();
            SqlParameter para_productId = new SqlParameter("@ma", (object)accountName);

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(para_productId);

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_KH_dk", db, lstParameter);
            if (rdr.HasRows)
                return ConvertRegisterModel(rdr).First();
            else
                throw new OperationCanceledException();
        }
    }
}
